package net.tv90.bfmccore.code;

import java.io.File;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;

public class VehicleMenuGenerator {

//    public static void main(String[] args) {
//        List<VehicleMenu> menus = new ArrayList<>();
//        VehicleMenuAssets.vehicles.forEach((K, V) -> {
//            menus.add(V);
//        });
//        generateMenu("Test", "RU", "AA", "A", menus);
//    }

    public static final String VEHICLE_MENU_ROOT = "./plugins/ChestCommands/menu/vehicle";

    public static void generateMenu(String world, String faction, String type, String pointIndex, List<VehicleMenu> menus) {
        StringBuilder str = new StringBuilder("menu-settings:\n" +
                "  name: \"" + faction + "部署点" + pointIndex + "\"\n" +
                "  rows: 6\n" +
                "  command: \"\"\n" +
                "  open-with-item:\n" +
                "    left-click: true\n" +
                "    right-click: true\n");
        int x = 1;
        int y = 1;
        for (VehicleMenu menu : menus) {
            String s = generate(menu)
                    .replace("{posX}", String.valueOf(x))
                    .replace("{posY}", String.valueOf(y))
                    .replace("{pointIndex}", pointIndex)
                    .replace("{type}", type);
            if (++x > 9) {
                y++;
                x = 1;
            }
            str.append(s);
        }
        try {
            File parent = new File(new File(VEHICLE_MENU_ROOT), "/" + world);
            if (!parent.exists()) {
                parent.mkdirs();
            }
            File file = new File(parent, world + "_" + faction + "_" + type.toUpperCase() + "_" + pointIndex + ".yml");
            try (Writer writer = new OutputStreamWriter(Files.newOutputStream(file.toPath()), StandardCharsets.UTF_8)) {
                writer.write(str.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static String generate(VehicleMenu menu) {
        String p = menu.getLore().get(menu.getLore().size() - 1);
        String permission = null;
        if (p.startsWith("%unlock")) {
            permission = p
                    .replace('_', '.')
                    .replace("%", "");
        }
        String str1 = "X{posX}Y{posY}:\n" +
                "  NAME: \"{name}\"\n";
        StringBuilder str2 = new StringBuilder("  LORE:\n");
        for (String s : menu.getLore()) {
            str2.append("    - '{content}'\n".replace("{content}", s));
        }
        StringBuilder sb3 = new StringBuilder();
//        if (permission != null) {
//            String pm = "  PERMISSION: {permission}\n  PERMISSION-MESSAGE: '&e[管理员]你还未解锁该载具！'\n";
//            sb3.append(pm.replace("{permission}", permission));
//        }
        String str3 =
                "  ID: mcheli_{itemId}\n" +
                        "  DATA-VALUE: 0\n" +
                        "  KEEP-OPEN: false\n" +
                        "  PRICE: 0\n" +
                        "  AUTO-REFRESH: 1\n" +
                        "  COMMAND: \"console:game deployv {player} {pointIndex} {type} {itemId}\"\n" +
                        "  POSITION-X: {posX}\n" +
                        "  POSITION-Y: {posY}\n";
        return (str1 + str2 + sb3 + str3)
                .replace("{name}", menu.getName())
                .replace("{itemId}", menu.getItemId())
                .replace("{coordX}", String.valueOf(menu.getDeployPos().x))
                .replace("{coordY}", String.valueOf(menu.getDeployPos().y))
                .replace("{coordZ}", String.valueOf(menu.getDeployPos().z));
    }
}
